﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FA_Commons
{
    public class CheckNull
    {
        public CheckNull() { }

        public static Boolean ToBoolean(Object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(obj);
            }
        }

        public static Boolean IntToBoolean(Object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return false;
            }
            else
            {
                if (Convert.ToInt32(obj) == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static string ToString(Object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return "";
            }
            else
            {
                return Convert.ToString(obj);
            }
        }

        public static Double ToDouble(Object obj)
        {
            if (obj == null || obj == DBNull.Value || obj.ToString() == "")
            {
                return 0;
            }
            else
            {
                return Convert.ToDouble(obj);
            }
        }

        public static float ToFloat(Object obj)
        {
            if (obj == null || obj == DBNull.Value || obj.ToString() == "")
            {
                return 0;
            }
            else
            {
                return Convert.ToSingle(obj);
            }
        }

        public static Int16 ToInt16(Object obj)
        {
            if (obj == null || obj == DBNull.Value || obj.ToString() == "")
            {
                return 0;
            }
            else
            {
                return Convert.ToInt16(obj);
            }
        }

        public static Int32 ToInt32(Object obj)
        {
            if (obj == null || obj == DBNull.Value || obj.ToString() == "")
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public static Int64 ToInt64(Object obj)
        {
            if (obj == null || obj == DBNull.Value || obj.ToString() == "")
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }

        public static Guid ToGuid(Object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return Guid.NewGuid();
            }
            else
            {
                return new System.Guid((byte[])obj);
            }
        }

        public static DateTime ToDate(Object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return Convert.ToDateTime(obj);
            }
            else
            {
                return Convert.ToDateTime(obj);
            }
        }

        public static int GuidToNull(Guid guid)
        {
            int count = 0;
            byte[] arrByte = guid.ToByteArray();
            for (int index = 0; index < arrByte.Length; index++)
            {
                if (arrByte.GetValue(index).ToString() != "0")
                {
                    count = count + 1;
                }
            }
            return count;
        }
    }
}
